import re

from django.contrib.auth.backends import ModelBackend
from rest_framework.pagination import PageNumberPagination

from xyz.models import User


def jwt_response_payload_handler(token, user=None, request=None):

    return {
        'mobile': user.mobile,
        'avatar': user.avatar,
        'token': token,
        'id': user.id,
        'username': user.username
    }


class UsernameMobileAuthentication(ModelBackend):

    def authenticate(self, request, username=None, password=None, **kwargs):
        '''重写认证函数:使其具有手机号的认证功能'''
        user = User.objects.get(mobile=username)

        if user and user.check_password(password):
            return user

class PageNumber(PageNumberPagination):

    page_size = 20

    max_page_size = 20

    # def get_paginated_response(self, data):
    #     return Response({
    #         'count':self.page.paginator.count,  #用户总量
    #         'lists':data,     # 分页的结果
    #         "page": self.page.number,   # 第几页
    #         "pages": self.page.paginator.num_pages,     # 总页数
    #         "pagesize": self.page_size   #页容量
    #     })